package org.example.quartz.task;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.service.LogService;
import org.example.quartz.service.OrgService;
import org.example.quartz.service.UtilService;
import org.example.quartz.util.DataSourceUtil;
import org.quartz.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
// 再Job被执行结束后，将会更新JobDataMap，这样下次Job执行后悔使用新的值而不是初始值
@PersistJobDataAfterExecution
// 同一时间将只有一个Job实例被执行，为了避免并发问题导致数据紊乱，建议这两个注解一起使用
@DisallowConcurrentExecution
public class AssistQcQuartz implements QuartzTaskJob {


    Map<String, Object> gsMap = DataSourceUtil.getGS();
    //全局接口配置
    String IP = (String) gsMap.get("IP");
    String PORT = (String) gsMap.get("PORT");
    String ECC = (String) gsMap.get("ECC");
    String Bearer = (String) gsMap.get("Bearer");

    //日志
    LogService loginfo = SpringUtil.getBean(LogService.class);
    //orgService
    OrgService orgService = SpringUtil.getBean(OrgService.class);
    //orgService
    UtilService utilService = SpringUtil.getBean(UtilService.class);
    /**
     * 辅助期初接口
     * 辅助余额：GSCloud文档3.12
     * http://ip:port/api/jg/syncbddata/v1.0/bdsyncinterface/syncFzye
     **/
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) {
        //传入变量 lastTime 最后抓取时间
        //链接BIP数据库，查询出需要同步的数据，根据TS时间进行判断。
        //TS从中间库表获取到最后一次更新时间，根据此时间进行增量查询推送；
        //中间库日志表记录推送日志，成功、失败、返回信息等（备查）
        //中间库记录最后推送数据的时间，根据时间进行下次查询的条件；
        //查询脚本
        String methodName = "AssistQcQuartz";
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
        //1、查询时间
        String ts = utilService.getTs(methodName, jdbcTemplate);
        String orgStr = utilService.getGSOrgStr(jdbcTemplate);
        /**
         *科目辅助余额接口
         * 1、业务逻辑
         * 根据传入科目及辅助期初余额数据同步至GSCloud系统科目及辅助期初余额中，该接口只做年初科目及辅助期初余额的初始。
         * 2、数据逻辑
         * 接口需传递明细科目及辅助期初余额全量数据，每次设置都是先删除再重新初始。每一个一级科目的所有明细科目及辅助期初余额数据作为调用一次接口的入参。
         *
         * */
        //2、根据时间查询数据集 (停用和删除的过滤掉，暂不考虑，线下处理)
        String selectSQL = "SELECT\n" +
                "\torg_accountingbook.code 账簿编码,\n" +
                "\torg_accountingbook.name 账簿名称,\n" +
                "\t'' as 科目编号,\n" +
                "\tbd_accasoa.dispname 科目名称,\n" +
                "\t'01' as 币种编号,\n" +
                "\tsum( CASE WHEN adjustperiod = '00' THEN gl_detail.localdebitamount ELSE 0 END ) \"期初借方\",\n" +
                "\tsum( CASE WHEN adjustperiod = '00' THEN gl_detail.localcreditamount ELSE 0 END ) \"期初贷方\",\n" +
                "\tsum( CASE WHEN adjustperiod = '12' THEN gl_detail.localdebitamount ELSE 0 END ) \"借方发生\",\n" +
                "\tsum( CASE WHEN adjustperiod = '12' THEN gl_detail.localcreditamount ELSE 0 END ) \"贷方发生\",\n" +
                "\tsum( CASE WHEN adjustperiod > '00' AND adjustperiod <= '12' THEN gl_detail.localdebitamount ELSE 0 END ) \"借方累计\",\n" +
                "\tsum( CASE WHEN adjustperiod > '00' AND adjustperiod <= '12' THEN gl_detail.localcreditamount ELSE 0 END ) \"贷方累计\",\n" +
                "\tsum( CASE WHEN adjustperiod <= '12' THEN gl_detail.localdebitamount ELSE 0 END ) \"借方期末\",\n" +
                "\tsum( CASE WHEN adjustperiod <= '12' THEN gl_detail.localcreditamount ELSE 0 END ) \"贷方期末\" \n" +
                "FROM\n" +
                "\tgl_detail gl_detail,\n" +
                "\torg_accountingbook,\n" +
                "\tbd_accasoa,\n" +
                "\tbd_account \n" +
                "WHERE\n" +
                "\tgl_detail.pk_accountingbook = org_accountingbook.pk_accountingbook \n" +
                "\tAND gl_detail.pk_accasoa = bd_accasoa.pk_accasoa \n" +
                "\tAND bd_accasoa.pk_account = bd_account.pk_account \n" +
                "\tAND gl_detail.yearv = '2024' \n" +
                "\tAND gl_detail.adjustperiod >= '00' \n" +
                "\tAND gl_detail.adjustperiod <= '12' \n" +
                "\tAND org_accountingbook.code = '1001-0001' \n" +
                "\tAND gl_detail.discardflagv <> 'Y' \n" +
                "\tAND gl_detail.dr = 0 \n" +
                "\tAND gl_detail.voucherkindv <> 255 \n" +
                "\tAND gl_detail.tempsaveflag <> 'Y' \n" +
                "\tAND gl_detail.voucherkindv <> 5 \n" +
                "\tAND 1 = 1 \n" +
                "GROUP BY\n" +
                "\tgl_detail.pk_accasoa,\n" +
                "\tgl_detail.pk_accountingbook,\n" +
                "\tgl_detail.yearv,\n" +
                "\torg_accountingbook.code,\n" +
                "\torg_accountingbook.name,\n" +
                "\tbd_accasoa.dispname";
        //查询数据，根据数据结果推送给GS浪潮
        Session oracleSession = DataSourceUtil.getOracleSession();
        List<Entity> query = oracleSession.query(selectSQL);
        //拼装数据，推送给浪潮
        if(query.size()>0){
            for (Map<String, Object> gSmap : query) {
                //更换gsCode
                String gsCode = orgService.selectGsCode((String) gSmap.get("ORGCODE"),jdbcTemplate);
                String GsJson ="{\n" +
                        "\t\"YEAR\": \"2024\",\n" +
                        "\t\"LEDGER\": \"01001\",\n" +
                        "\t\"ASSDATALIST\": [{\n" +
                        "\t\t\"ACCTITLECODE\": \"1001\",\n" +
                        "\t\t\"CURRENCY\": \"01\",\n" +
                        "\t\t\"BEGINBALANCEAMT\": \"0\",\n" +
                        "\t\t\"CURCUMDRAMT\": \"0\",\n" +
                        "\t\t\"CURCUMCRAMT\": \"0\",\n" +
                        "\t\t\"DEBITAMT\": \"0\",\n" +
                        "\t\t\"CREDITAMT\": \"0\",\n" +
                        "\t\t\"ENDBALANCEAMT\": \"0\",\n" +
                        "\t\t\"BEGINBALANCEFOR\": \"0\",\n" +
                        "\t\t\"CURCUMDRFOR\": \"0\",\n" +
                        "\t\t\"CURCUMCRFOR\": \"0\",\n" +
                        "\t\t\"DEBITFOR\": \"0\",\n" +
                        "\t\t\"CREDITFOR\": \"0\",\n" +
                        "\t\t\"ENDBALANCEFOR\": \"0\",\n" +
                        "\t\t\"BEGINBALANCEQTY\": \"0\",\n" +
                        "\t\t\"CURCUMDRQTY\": \"0\",\n" +
                        "\t\t\"CURCUMCRQTY\": \"0\",\n" +
                        "\t\t\"DEBITQTY\": \"0\",\n" +
                        "\t\t\"CREDITQTY\": \"0\",\n" +
                        "\t\t\"ENDBALANCEQTY\": \"0\",\n" +
                        "\t\t\"ORDERNUMBER\": \"1\",\n" +
                        "\t\t\"DEPTCODE\": \"\",\n" +
                        "\t\t\"ACCEMPLOYEECODE\": \"\",\n" +
                        "\t\t\"RELATEDORGCODE\": \"\",\n" +
                        "\t\t\"SPECATECODE01\": \"\",\n" +
                        "\t\t\"SPECATECODE02\": \"\",\n" +
                        "\t\t\"SPECATECODE03\": \"\",\n" +
                        "\t\t\"SPECATECODE04\": \"\",\n" +
                        "\t\t\"SPECATECODE05\": \"\",\n" +
                        "\t\t\"SPECATECODE06\": \"\",\n" +
                        "\t\t\"SPECATECODE07\": \"\",\n" +
                        "\t\t\"SPECATECODE08\": \"\",\n" +
                        "\t\t\"SPECATECODE09\": \"\",\n" +
                        "\t\t\"SPECATECODE10\": \"\",\n" +
                        "\t\t\"SPECATECODE11\": \"\",\n" +
                        "\t\t\"SPECATECODE12\": \"\",\n" +
                        "\t\t\"SPECATECODE13\": \"\",\n" +
                        "\t\t\"SPECATECODE14\": \"\",\n" +
                        "\t\t\"SPECATECODE15\": \"\",\n" +
                        "\t\t\"SPECATECODE16\": \"\",\n" +
                        "\t\t\"SPECATECODE17\": \"\",\n" +
                        "\t\t\"SPECATECODE18\": \"\",\n" +
                        "\t\t\"SPECATECODE19\": \"\",\n" +
                        "\t\t\"SPECATECODE20\": \"\",\n" +
                        "\t\t\"SPECATECODE21\": \"\",\n" +
                        "\t\t\"SPECATECODE22\": \"\",\n" +
                        "\t\t\"SPECATECODE23\": \"\",\n" +
                        "\t\t\"SPECATECODE24\": \"\",\n" +
                        "\t\t\"SPECATECODE25\": \"\",\n" +
                        "\t\t\"SPECATECODE26\": \"\",\n" +
                        "\t\t\"SPECATECODE27\": \"\",\n" +
                        "\t\t\"SPECATECODE28\": \"\",\n" +
                        "\t\t\"SPECATECODE29\": \"\",\n" +
                        "\t\t\"SPECATECODE30\": \"\",\n" +
                        "\t\t\"SPECATECODE31\": \"\",\n" +
                        "\t\t\"SPECATECODE32\": \"\",\n" +
                        "\t\t\"SPECATECODE33\": \"\",\n" +
                        "\t\t\"SPECATECODE34\": \"\",\n" +
                        "\t\t\"SPECATECODE35\": \"\",\n" +
                        "\t\t\"SPECATECODE36\": \"\",\n" +
                        "\t\t\"SPECATECODE37\": \"\",\n" +
                        "\t\t\"SPECATECODE38\": \"\",\n" +
                        "\t\t\"SPECATECODE39\": \"\",\n" +
                        "\t\t\"SPECATECODE40\": \"\",\n" +
                        "\t\t\"SPECATECODE41\": \"\",\n" +
                        "\t\t\"SPECATECODE42\": \"\",\n" +
                        "\t\t\"SPECATECODE43\": \"\",\n" +
                        "\t\t\"SPECATECODE44\": \"\",\n" +
                        "\t\t\"SPECATECODE45\": \"\",\n" +
                        "\t\t\"SPECATECODE46\": \"\",\n" +
                        "\t\t\"SPECATECODE47\": \"\",\n" +
                        "\t\t\"SPECATECODE48\": \"\",\n" +
                        "\t\t\"SPECATECODE49\": \"\",\n" +
                        "\t\t\"SPECATECODE50\": \"\",\n" +
                        "\t\t\"SPECATECODE51\": \"\",\n" +
                        "\t\t\"SPECATECODE52\": \"\",\n" +
                        "\t\t\"SPECATECODE53\": \"\",\n" +
                        "\t\t\"SPECATECODE54\": \"\",\n" +
                        "\t\t\"SPECATECODE55\": \"\",\n" +
                        "\t\t\"SPECATECODE56\": \"\",\n" +
                        "\t\t\"SPECATECODE57\": \"\",\n" +
                        "\t\t\"SPECATECODE58\": \"\",\n" +
                        "\t\t\"SPECATECODE59\": \"\",\n" +
                        "\t\t\"SPECATECODE60\": \"\",\n" +
                        "\t\t\"SPECATECODE61\": \"\",\n" +
                        "\t\t\"SPECATECODE62\": \"\",\n" +
                        "\t\t\"SPECATECODE63\": \"\",\n" +
                        "\t\t\"SPECATECODE64\": \"\",\n" +
                        "\t\t\"SPECATECODE65\": \"\",\n" +
                        "\t\t\"SPECATECODE66\": \"\",\n" +
                        "\t\t\"SPECATECODE67\": \"\",\n" +
                        "\t\t\"SPECATECODE68\": \"\",\n" +
                        "\t\t\"SPECATECODE69\": \"\",\n" +
                        "\t\t\"SPECATECODE70\": \"\",\n" +
                        "\t\t\"SPECATECODE71\": \"\",\n" +
                        "\t\t\"SPECATECODE72\": \"\",\n" +
                        "\t\t\"SPECATECODE73\": \"\",\n" +
                        "\t\t\"SPECATECODE74\": \"\",\n" +
                        "\t\t\"SPECATECODE75\": \"\",\n" +
                        "\t\t\"SPECATECODE76\": \"\",\n" +
                        "\t\t\"SPECATECODE77\": \"\",\n" +
                        "\t\t\"SPECATECODE78\": \"\",\n" +
                        "\t\t\"SPECATECODE79\": \"\",\n" +
                        "\t\t\"SPECATECODE80\": \"\",\n" +
                        "\t\t\"SPECATECODE81\": \"\",\n" +
                        "\t\t\"SPECATECODE82\": \"\",\n" +
                        "\t\t\"SPECATECODE83\": \"\",\n" +
                        "\t\t\"SPECATECODE84\": \"\",\n" +
                        "\t\t\"SPECATECODE85\": \"\",\n" +
                        "\t\t\"SPECATECODE86\": \"\",\n" +
                        "\t\t\"SPECATECODE87\": \"\",\n" +
                        "\t\t\"SPECATECODE88\": \"\",\n" +
                        "\t\t\"SPECATECODE89\": \"\",\n" +
                        "\t\t\"SPECATECODE90\": \"\",\n" +
                        "\t\t\"SPECATECODE91\": \"\",\n" +
                        "\t\t\"SPECATECODE92\": \"\",\n" +
                        "\t\t\"SPECATECODE93\": \"\",\n" +
                        "\t\t\"SPECATECODE94\": \"\",\n" +
                        "\t\t\"SPECATECODE95\": \"\",\n" +
                        "\t\t\"SPECATECODE96\": \"\",\n" +
                        "\t\t\"SPECATECODE97\": \"\",\n" +
                        "\t\t\"SPECATECODE98\": \"\",\n" +
                        "\t\t\"SPECATECODE99\": \"\",\n" +
                        "\t\t\"BIZDICTCODE01\": \"\",\n" +
                        "\t\t\"BIZDICTCODE02\": \"\",\n" +
                        "\t\t\"BIZDICTCODE03\": \"\",\n" +
                        "\t\t\"BIZDICTCODE04\": \"\",\n" +
                        "\t\t\"BIZDICTCODE05\": \"\",\n" +
                        "\t\t\"BIZDICTCODE06\": \"\",\n" +
                        "\t\t\"BIZDICTCODE07\": \"\",\n" +
                        "\t\t\"BIZDICTCODE08\": \"\",\n" +
                        "\t\t\"BIZDICTCODE09\": \"\",\n" +
                        "\t\t\"BIZDICTCODE10\": \"\",\n" +
                        "\t\t\"BIZDICTCODE11\": \"\",\n" +
                        "\t\t\"BIZDICTCODE12\": \"\",\n" +
                        "\t\t\"BIZDICTCODE13\": \"\",\n" +
                        "\t\t\"BIZDICTCODE14\": \"\",\n" +
                        "\t\t\"BIZDICTCODE15\": \"\",\n" +
                        "\t\t\"BIZDICTCODE16\": \"\",\n" +
                        "\t\t\"BIZDICTCODE17\": \"\",\n" +
                        "\t\t\"BIZDICTCODE18\": \"\",\n" +
                        "\t\t\"BIZDICTCODE19\": \"\",\n" +
                        "\t\t\"BIZDICTCODE20\": \"\",\n" +
                        "\t\t\"BIZDICTCODE21\": \"\",\n" +
                        "\t\t\"BIZDICTCODE22\": \"\",\n" +
                        "\t\t\"BIZDICTCODE23\": \"\",\n" +
                        "\t\t\"BIZDICTCODE24\": \"\",\n" +
                        "\t\t\"BIZDICTCODE25\": \"\",\n" +
                        "\t\t\"BIZDICTCODE26\": \"\",\n" +
                        "\t\t\"BIZDICTCODE27\": \"\",\n" +
                        "\t\t\"BIZDICTCODE28\": \"\",\n" +
                        "\t\t\"BIZDICTCODE29\": \"\",\n" +
                        "\t\t\"BIZDICTCODE30\": \"\",\n" +
                        "\t\t\"BIZDICTCODE31\": \"\",\n" +
                        "\t\t\"BIZDICTCODE32\": \"\",\n" +
                        "\t\t\"BIZDICTCODE33\": \"\",\n" +
                        "\t\t\"BIZDICTCODE34\": \"\",\n" +
                        "\t\t\"BIZDICTCODE35\": \"\",\n" +
                        "\t\t\"BIZDICTCODE36\": \"\",\n" +
                        "\t\t\"BIZDICTCODE37\": \"\",\n" +
                        "\t\t\"BIZDICTCODE38\": \"\",\n" +
                        "\t\t\"BIZDICTCODE39\": \"\",\n" +
                        "\t\t\"BIZDICTCODE40\": \"\",\n" +
                        "\t\t\"BIZDICTCODE41\": \"\",\n" +
                        "\t\t\"BIZDICTCODE42\": \"\",\n" +
                        "\t\t\"BIZDICTCODE43\": \"\",\n" +
                        "\t\t\"BIZDICTCODE44\": \"\",\n" +
                        "\t\t\"BIZDICTCODE45\": \"\",\n" +
                        "\t\t\"BIZDICTCODE46\": \"\",\n" +
                        "\t\t\"BIZDICTCODE47\": \"\",\n" +
                        "\t\t\"BIZDICTCODE48\": \"\",\n" +
                        "\t\t\"BIZDICTCODE49\": \"\",\n" +
                        "\t\t\"BIZDICTCODE50\": \"\",\n" +
                        "\t\t\"BIZDICTCODE51\": \"\",\n" +
                        "\t\t\"BIZDICTCODE52\": \"\",\n" +
                        "\t\t\"BIZDICTCODE53\": \"\",\n" +
                        "\t\t\"BIZDICTCODE54\": \"\",\n" +
                        "\t\t\"BIZDICTCODE55\": \"\",\n" +
                        "\t\t\"BIZDICTCODE56\": \"\",\n" +
                        "\t\t\"BIZDICTCODE57\": \"\",\n" +
                        "\t\t\"BIZDICTCODE58\": \"\",\n" +
                        "\t\t\"BIZDICTCODE59\": \"\",\n" +
                        "\t\t\"BIZDICTCODE60\": \"\",\n" +
                        "\t\t\"BIZDICTCODE61\": \"\",\n" +
                        "\t\t\"BIZDICTCODE62\": \"\",\n" +
                        "\t\t\"BIZDICTCODE63\": \"\",\n" +
                        "\t\t\"BIZDICTCODE64\": \"\",\n" +
                        "\t\t\"BIZDICTCODE65\": \"\",\n" +
                        "\t\t\"BIZDICTCODE66\": \"\",\n" +
                        "\t\t\"BIZDICTCODE67\": \"\",\n" +
                        "\t\t\"BIZDICTCODE68\": \"\",\n" +
                        "\t\t\"BIZDICTCODE69\": \"\",\n" +
                        "\t\t\"BIZDICTCODE70\": \"\",\n" +
                        "\t\t\"BIZDICTCODE71\": \"\",\n" +
                        "\t\t\"BIZDICTCODE72\": \"\",\n" +
                        "\t\t\"BIZDICTCODE73\": \"\",\n" +
                        "\t\t\"BIZDICTCODE74\": \"\",\n" +
                        "\t\t\"BIZDICTCODE75\": \"\",\n" +
                        "\t\t\"BIZDICTCODE76\": \"\",\n" +
                        "\t\t\"BIZDICTCODE77\": \"\",\n" +
                        "\t\t\"BIZDICTCODE78\": \"\",\n" +
                        "\t\t\"BIZDICTCODE79\": \"\",\n" +
                        "\t\t\"BIZDICTCODE80\": \"\",\n" +
                        "\t\t\"BIZDICTCODE81\": \"\",\n" +
                        "\t\t\"BIZDICTCODE82\": \"\",\n" +
                        "\t\t\"BIZDICTCODE83\": \"\",\n" +
                        "\t\t\"BIZDICTCODE84\": \"\",\n" +
                        "\t\t\"BIZDICTCODE85\": \"\",\n" +
                        "\t\t\"BIZDICTCODE86\": \"\",\n" +
                        "\t\t\"BIZDICTCODE87\": \"\",\n" +
                        "\t\t\"BIZDICTCODE88\": \"\",\n" +
                        "\t\t\"BIZDICTCODE89\": \"\",\n" +
                        "\t\t\"BIZDICTCODE90\": \"\",\n" +
                        "\t\t\"BIZDICTCODE91\": \"\",\n" +
                        "\t\t\"BIZDICTCODE92\": \"\",\n" +
                        "\t\t\"BIZDICTCODE93\": \"\",\n" +
                        "\t\t\"BIZDICTCODE94\": \"\",\n" +
                        "\t\t\"BIZDICTCODE95\": \"\",\n" +
                        "\t\t\"BIZDICTCODE96\": \"\",\n" +
                        "\t\t\"BIZDICTCODE97\": \"\",\n" +
                        "\t\t\"BIZDICTCODE98\": \"\",\n" +
                        "\t\t\"BIZDICTCODE99\": \"\"\n" +
                        "\t}]\n" +
                        "}";
                GsJson = GsJson.replace("null","");
                //调用GS接口，推送给GS浪潮，获取返回值;
                //1、推送用户档案
                String gsUrl = IP+":"+PORT+"/api/jg/syncbddata/v1.0/bdsyncinterface/syncFzye";
                String returnMSG =   HttpRequest.post(gsUrl)
                        .header("Content-Type", "application/json")
                        .header("Accept-Language", "zh-CHS")
                        .header("X-ECC-Current-Tenant", ECC)
                        .header("Authorization", Bearer).timeout(200000)
                        .body(GsJson).execute().body();
                if(!returnMSG.isEmpty()){
                    JSONObject returnObj = JSONObject.parseObject(returnMSG);
                    //成功
                    if(returnObj.get("code").equals("0") ){
                        int loginsert = loginfo.loginfo(jdbcTemplate,methodName, LocalDateTime.now().toString(),"成功",GsJson,returnMSG,"","",gsUrl, (String) gSmap.get("TS"));
                        if(loginsert>0){
                            //System.out.println("日志写入成功！！");
                        }else{
                            System.out.println("日志写入失败！！");
                        }
                        //失败
                    }else if(returnObj.get("code").equals("1") ){
                        //写入日志表
                        int loginsert = loginfo.loginfo(jdbcTemplate,methodName,LocalDateTime.now().toString(),"失败",GsJson,returnMSG,"","",gsUrl, (String) gSmap.get("TS"));
                        if(loginsert>0){
                            System.out.println("日志写入成功！！");
                        }else{
                            System.out.println("日志写入失败！！");
                        }
                    }else if(returnObj.get("code")==null && ("").equals(returnObj.get("code"))){
                        //写入日志表
                        int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", GsJson, returnMSG, "",  (String) gSmap.get("ID"), gsUrl, (String) gSmap.get("TS"));
                        if (loginsert > 0) {
                            System.out.println("日志写入成功！！");
                        } else {
                            System.out.println("日志写入失败！！");
                        }
                    }
                }
            }
        }else{
            System.out.println("暂无新数据推送");
        }

    }

}
